import { ref } from 'vue';

export default function useCity(city, api_key, error) {
  const weather = ref({});
  const fetchWeather = () => {
    return new Promise((resolve, reject) => {
      fetch(
        `https://devapi.qweather.com/v7/weather/now?location=${city.id}&key=${api_key}`
      )
        .then((res) => res.json())
        .then((res) => {
          if (res.code === '200') {
            weather.value = res.now;
            error.state = false;
            resolve();
          } else {
            error.state = true;
            error.msg = '获取天气数据失败';
            reject(false);
          }
        });
    });
  };
  return {
    weather,
    fetchWeather,
  };
}
